package org.grottarossa.ui.core.controls.document;

import java.util.ResourceBundle;

import org.grottarossa.template.data.pojo.content.DocumentList;
import org.grottarossa.ui.core.ApplicationSession;
import org.grottarossa.ui.core.actions.CustomActionHandlerForContextMenu;
import org.grottarossa.ui.core.base.BasePanelView;
import org.grottarossa.ui.core.container.UserListContainer;
import org.grottarossa.ui.core.controls.document.upload.DocumentUploader;

import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.terminal.ExternalResource;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.AbstractComponent;


/***
 * Controllo per la gestione delle versioni di un documento
 * @author 
 *
 */
public class DocumentVersionsManager extends BasePanelView<DocumentVersionsManagerPresenter>
implements ItemClickListener
{
	/*** Tabella delle versioni ***/
	private DocumentVersionsTable m_versionsTable;

	public DocumentVersionsManager(ApplicationSession appSession, ResourceBundle localizer)
	{
		super(appSession, localizer);
		initializeComponents();
	}

	@Override
	protected void initializeComponents()
	{
		getLayout().setWidth(100, Sizeable.UNITS_PERCENTAGE);
		getLayout().setHeight(100, Sizeable.UNITS_PERCENTAGE);
		m_versionsTable = new DocumentVersionsTable();
		m_versionsTable.setWidth(100, Sizeable.UNITS_PERCENTAGE);
		m_versionsTable.setHeight(100, Sizeable.UNITS_PERCENTAGE);
		m_versionsTable.setSelectable(true);
		m_versionsTable.addListener(this);
		addComponent(m_versionsTable);
	}

	/***
	 * Popola la tabella delle versioni
	 * @param versions
	 */
	public void fillVersionsTable(DocumentList versions)
	{
		m_versionsTable.setContainerDataSource(new DocumentVersionsContainer(versions));

		m_versionsTable.setVisible(true);

		m_versionsTable.requestRepaintRequests();
		requestRepaintRequests();

		addComponent(m_versionsTable);
//		m_versionsTable.setVersions(new DocumentVersionsContainer(versions));
	}

	/***
	 * Aggiunge il menu delle action alla tabella delle versioni
	 * @param actionsHandler
	 */
	public void setTableMenu(CustomActionHandlerForContextMenu actionsHandler)
	{
		m_versionsTable.removeAllActionHandlers();
		m_versionsTable.addActionHandler(actionsHandler);
	}

	public void itemClick(ItemClickEvent event)
	{
		Integer versionId = (Integer)event.getItem().getItemProperty(DocumentVersionsContainer.VERSION).getValue();
		String versionName = (String)event.getItem().getItemProperty(DocumentVersionsContainer.NAME).getValue();

		if(event.getButton() == ItemClickEvent.BUTTON_RIGHT)
			m_presenter.rightClickOnItem(versionId, versionName);
	}


}
